//
//  list_join.hpp
//  cpp_algorithm
//
//  Created by 李贺 on 2021/12/2.
//

#ifndef list_join_hpp
#define list_join_hpp

#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;

class ListNode {
        
public:
    int val;
    ListNode *next;
    ListNode(int x): val(x),next(NULL){}
};

class Solution_list_join {
    
public:
    
    ListNode *checkJoin(ListNode *headA, ListNode *headB) {
        
        ListNode *tempHeadB = headB;
        
        ListNode *curA = headA;
        ListNode *curB = headB;
        
        for (; curA != nullptr; curA = curA->next) {
            curB = tempHeadB;
            for (; curB != nullptr; curB = curB->next) {
                if (curA == curB) {
                    return curA;
                }
            }
        }
        
        return nullptr;
    }
    
    void test() {
        ListNode * headA = new ListNode(88);
        ListNode * headB = new ListNode(99);
        
        ListNode * node1 = new ListNode(1);
        ListNode * node2 = new ListNode(2);
        ListNode * node3 = new ListNode(3);
        ListNode * node4 = new ListNode(4);
        ListNode * node5 = new ListNode(5);
        ListNode * node6 = new ListNode(6);
        
        headA->next = node1;
        node1->next = node2;
        node2->next = node3;
        node3->next = node4;
        node4->next = node5;
        node5->next = node6;
        
        
        headB->next = node4;
        node4->next = node5;
        node5->next = node6;
        
        ListNode * res = this->checkJoin(headA, headB);
        cout << res->val << endl;
        
    }
    
};

#endif /* list_join_hpp */
